namespace Trader.Analyzing
{
    using System;
    using System.Collections.Generic;
    using BO.Interface;
    using DataServices.Interface;
    using Indicators.Interface;
    using Interface;

    public class StockSignalScenariosGenerator : IStockSignalScenariosGenerator
    {
        private readonly decimal minChangeExpected;
        private readonly ISignalsCache signalsCache;
        private readonly IList<ISignal> signals;

        public StockSignalScenariosGenerator(IList<ISignal> signals, ISignalsCache signalsCache,
                                             decimal minChangeExpected)
        {
            this.signals = signals;
            this.signalsCache = signalsCache;
            this.minChangeExpected = minChangeExpected;
        }

        #region IStockSignalScenariosGenerator Members

        public IDictionary<ISignal, IScenarios> GenerateScenraios(IStock stock, DateTime tradingDate)
        {
            IList<ISignal> indicators = signals;

            var indicatorsScenarios = new Dictionary<ISignal, IScenarios>();

            var scenariosGenerator = new ScenariosGenerator(5);
            foreach (ISignal indicator in indicators)
            {
                IActionPoints actionPoints =
                    new StockSignalSimulator(stock, indicator, signalsCache).Simulate(tradingDate);
                int minRequiredOccurances = Math.Max((Math.Max(2000, tradingDate.Year) - 2000 + 1)*2/3, 1);
                if (actionPoints.Count < minRequiredOccurances)
                {
                    continue;
                }

                IScenarios scenarios = scenariosGenerator.Generate(stock, actionPoints);
                if (scenarios.GetExpectedGain() >= minChangeExpected/100M)
                {
                    indicatorsScenarios.Add(indicator, scenarios);
                }
            }

            return indicatorsScenarios;
        }

        #endregion
    }
}